/**
 *	A static class for easy handling of browser cookies.
 *	The class provides functionality to save, load and 
 *	delete cookie-based information.
 *
 *	@author		Henrik Andersen
 *	@email		henrik.andersen@lnu.se
 *	@version	1.0
 *	@since		xxxx-xx-xx
 *	@requires	
 */
var Cookie = {

	//---------------------------------------------------
	//  Public static methods
	//---------------------------------------------------
	
	/**
	 *	Method to store a string-based value in a cookie. 
	 *	The method can save strings and numbers, but both 
	 *	will be handled as strings.
	 *
	 *	@param	name	The name of the cookie.
	 *	@param	value	The value of the cookie.
	 *	@param	days	The time in days it takes before the 
	 *					cookie expires. @default 100
	 *
	 *	@return undefined
	 */
	save : function(name, value, days)
	{
		if (isNaN(days))
			days = 100;
		
		document.cookie = name + "=" + value + ";expires=" + Cookie.expireDate(days);
	},
	
	/**
	 *	This method uses string manipulation to pick out specefik 
	 *	information stored in the cookie-string. If the information 
	 *	can not be located in the string, null is returned as 
	 *	a value.
	 *
	 *	@param	name	The name of the cookie to load.
	 *
	 *	@return String
	 */
	load : function(name)
	{
		var start = document.cookie.indexOf(name);
		
		if (start > -1)
		{
			var start	= start + name.length + 1;
			var end		= document.cookie.indexOf(";", start);
			
			if (end < start)
				var end = document.cookie.length;
				
			var valueStr = document.cookie.substring(start,end);
			
			return valueStr;
		}
		else 
		{
			return null;
		}
	},
	
	/**
	 *	Sets a specific cookie expiration date to zero so that 
	 *	the browser automatically deletes the data ie, the cookie.
	 *
	 *	@param	name	The name of the cookie to remove.
	 *
	 *	@return undefined
	 */
	remove : function(name)
	{
		document.cookie = name + "=;expires=" + Cookie.expireDate(0);
	},
	
	/**
	 *	Calculates the current date, adds the duration and 
	 *	convert the sum to milliseconds. The result is returned 
	 *	as a UTC string.
	 *
	 *	@param	days	The number of days the cookie should be valid.
	 *
	 *	@return String
	 */
	expireDate : function(days)
	{
		var date = new Date();
			date.setTime(date.getTime() + (1000 * 86400 * days));
			
		return date.toUTCString();
	}
}